package com.javacode2018.demo1;


import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class Demo1Test {

    @Test
    public void test1() throws Exception {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig1.class);
        UserService userService = context.getBean(UserService.class);
        userService.insert();
    }
    @Test
    public void test2() throws Exception {
        System.out.println(List.class.isAssignableFrom(ArrayList.class));
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig1.class);
        UserService1 userService = context.getBean(UserService1.class);
        userService.insert();
    }
    @Test
    public void test3() throws NoSuchMethodException {
        System.out.println(AnnotatedElementUtils.findMergedAnnotationAttributes(
                UserService1.class.getDeclaredMethod("insert"), Transactional.class, false, false));
    }

    interface C1<T>{
        void m1(T t);
    }
    static class C2 implements C1<String>{
        @Override
        public void m1(String s) {

        }
    }
    @Test
    public void test4() throws NoSuchMethodException {
        for (Method method : C2.class.getMethods()) {
            System.out.println(method+" "+method.isBridge());
        }

        System.out.println(BridgeMethodResolver.findBridgedMethod(C2.class.getMethod("m1", Object.class)));
    }
    @Test
    public void test5() throws NoSuchMethodException {
        System.out.println(AnnotatedElementUtils.findMergedAnnotationAttributes(
                UserService1.class, Transactional.class, false, false));
        System.out.println(AnnotatedElementUtils.findMergedAnnotationAttributes(
                UserService1.class.getDeclaredMethod("insert"), Transactional.class, false, false));
    }

    @Test
    public void test6() throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig1.class);
        UserService1 userService = context.getBean(UserService1.class);
        userService.insert1();
    }

    static class T1{
        public void m1(){
            System.out.println("T1 m1()");
            m2();
        }
        public void m2(){
            System.out.println("T1 m2()");
        }
    }

    static class T2 extends T1{

        public void m2(){
            System.out.println("T2 m2()");
        }
    }

    @Test
    public void test7() throws IOException {
        T2 t2 = new T2();
        t2.m1();
    }
}
